package com.cloud.gatewaySpringSecurity.handler;

import com.cloud.gatewaySpringSecurity.utils.SaveMessage;
import org.springframework.http.HttpStatus;
import org.springframework.http.server.reactive.ServerHttpResponse;
import org.springframework.security.access.AccessDeniedException;
import org.springframework.security.web.server.authorization.ServerAccessDeniedHandler;
import org.springframework.stereotype.Component;
import org.springframework.web.server.ServerWebExchange;
import reactor.core.publisher.Mono;

/**
 * @author zhozixiong
 * @date 2022/09/04
 *
 * 权限不够处理器
 */
@Component
public class AccessDeniedHandler implements ServerAccessDeniedHandler {

    @Override
    public Mono<Void> handle(ServerWebExchange serverWebExchange, AccessDeniedException e) {
        ServerHttpResponse response = serverWebExchange.getResponse();
        return SaveMessage.save(response, HttpStatus.BAD_REQUEST, "无权限访问");
    }
}
